﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;

namespace ExaminationSys.View.Section
{
    public partial class SectionDownloadEdit : System.Web.UI.UserControl
    {
        private static readonly SectionDownloadService Proxy = ObjectFactory<SectionDownloadService>.Create();
        private static SectionDownload entity = new SectionDownload();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                entity = null;
                Bind();
            }
        }
        private void Bind()
        {

            //SectionType.DataSource = CommonEnum.EnumToDictionary(typeof(EnumSectionType));
            //this.SectionTypeID.GetStore().DataBind();
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }
        public string SectionID { get; set; }


        public void SetData(string id,string sectionId="")
        {
            SectionDownloadEditPanel.Reset();
            if (!string.IsNullOrEmpty(id))
            {
                var realId = new Guid(id);
                var SectionDownload = Proxy.Get(realId);

                if (SectionDownload != null)
                {
                    entity = SectionDownload;
                    OrderIndex.Text = SectionDownload.OrderIndex.ToString();
                    Title.Text = SectionDownload.Title;
                    Description.Text = SectionDownload.Description;
                    if (!string.IsNullOrEmpty(SectionDownload.AttachFile))
                    {
                        //AttachFile.Visible = true;
                       // AttachFile.Visible = false;
                        reUploadBtn.Visible = true;
                    }
                    //AttachFile.Text = SectionDownload.AttachFile;

                    SectionID = SectionDownload.SectionID.ToString();
                    IDHidden.Value = SectionDownload.ID.ToString();
                    X.Js.Call("setID", SectionDownload.ID, SectionDownload.SectionID);

                    // X.Js.Call("setSectionID", SectionDownload.SectionID);
                    //HidSectionID.Value = SectionDownload.SectionID.ToString() ;
                }
            }
            else
            {
                entity = new SectionDownload();
                fileUpload.Clear();
                fileUpload.Reset();


                Title.Text = "";
               // AttachFile.Text = "";
                //AttachFile.Visible = false;
                fileUpload.Visible = true;
                reUploadBtn.Visible = false;
                IDHidden.Value = "";
                X.Js.Call("setSectionID", SectionID);

            }
         
            //X.Js.Call("setSectionID", SectionID);




        }

        public void Show()
        {
            this.SectionDownloadEditWindow.Show();
        }

        protected void reUploadBtn_Click(object sender, DirectEventArgs e)
        {
            //AttachFile.Visible = false;
            fileUpload.Visible = true;

        }

        protected void SaveSectionDownload(object sender, DirectEventArgs e)
        {
        
            string title = Title.Text;
            //string attachFile = AttachFile.Text;
            int orderIndex = int.Parse(OrderIndex.Text);
            var opeator = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(opeator))
            {
                Response.Redirect("~/Account/Login.aspx");
                return;
            }
            string description = Description.Text;
            DateTime OpeatorTime = DateTime.Now;
            int status = 1;
            string sectionid = HidSectionID.Value;

            if (string.IsNullOrEmpty(title))
            {
                MessageBoxHelper.ShowWarning("标题不能为空");
                return;
            }


            //entity.AttachFile = attachFile;
            if (fileUpload.HasFile)
            {
                string FileName = this.fileUpload.FileName.Trim();
                string FilePath = "/DownLoad/" + FileName;
                System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
                System.Web.HttpPostedFile _postedFile = _files[0];

                if (!string.IsNullOrEmpty(FilePath))
                {
                    if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(FilePath)))
                    {
                        System.IO.File.Delete(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                    }
                    _postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(FilePath));
                }

                entity.AttachFile = FilePath;
            }
            entity.Description = description;
            entity.Title = title;
            entity.OrderIndex = orderIndex;
            entity.SectionID = new Guid(sectionid);
           
            entity.Status = status;
            entity.Opeator = opeator;
            entity.OpeatorTime = DateTime.Now;
            var sucess = false;
            // entity.
            if (string.IsNullOrWhiteSpace(IDHidden.Value))
            {
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                SectionDownloadEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }

    }
}